package com.zzxy.mybatis.test;

import com.zzxy.mybatis.entity.Order;
import com.zzxy.mybatis.entity.OrderDetail;
import com.zzxy.mybatis.entity.User;
import com.zzxy.mybatis.mapper.UserMapper;
import com.zzxy.mybatis.vo.QueryVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class testUserMapper {

    InputStream is;
    SqlSessionFactory sessionFactory;
    SqlSession session;


    @Before
    public void before() throws IOException {
         is = Resources.getResourceAsStream("SqlMapConfig.xml");

        //b)通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂。
         sessionFactory = new SqlSessionFactoryBuilder().build(is);

        // c)通过SqlSessionFactory创建SqlSession。
         session = sessionFactory.openSession();
    }

    @After
    public void after(){
        session.close();
    }



    @Test
    public void test1(){//        a)读取配置文件；
        //查询一条结果
        User user = session.selectOne("findUserById", 1);
        System.out.println(user);//        关闭SqlSession。
    }

    @Test
    public void insert(){//        a)读取配置文件；

//        d)调用SqlSession的操作数据库方法。
        //查询一条结果
        User user = new User();
        user.setUsername("张三");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("枣庄");
        System.out.println(user);//        关闭SqlSession。
        session.insert("insertUser",user);
        System.out.println("行数"+user.getId());//获取插入的行数
        session.commit();//提交sql结果,查询不用提交

    }

    @Test
    public void delete() {//        a)读取配置文件；
        //查询一条结果
        session.delete("deleteByid",27);
        session.commit();//提交sql结果,查询不用提交
    }

    @Test
    public void update(){//        a)读取配置文件；
        User user =new User();
        user.setId(1);
        user.setUsername("王五");
        user.setSex("男");
        user.setAddress("曹县");
        session.update("updateUsername",user);
        session.commit();//提交sql结果,查询不用提交
    }


    @Test
    public void selectList() {//        a)读取配置文件；
        List<User> list=session.selectList("selectList","王");
        System.out.println(list);
        session.commit();//提交sql结果,查询不用提交
    }


    @Test
    public void testMapperFindUserById(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user1=mapper.findUserById(1);
        System.out.println("输出第一行:"+user1);


    }

    @Test
    public void testMapperSave(){
        UserMapper mapper = session.getMapper(UserMapper.class);

        User user=new User();
        user.setUsername("李四");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("曹县");
        mapper.save(user);
        session.commit();

    }

    @Test
    public void testMapperFindUserListByVo(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user=new User();
        user.setId(10);
        Order order=new Order();
        order.setNumber("1000012");
        QueryVo vo=new QueryVo();
        vo.setUser(user);
        vo.setOrder(order);
        List<User> user1= mapper.findUserListByVo(vo);
        System.out.println(user1);
    }

    @Test
    public void testMapperUpdate(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user=new User();
        user.setId(10);
        user.setAddress("宇宙中心曹县");
        mapper.updatetUser(user);
        session.commit();
    }


    @Test
    public void testMapperfindUserListByMap(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        Map<String, Object> map = new HashMap<>();
        map.put("sex1","男");
        map.put("name","张");
        List<User> user=mapper.findUserListByMap(map);
        System.out.println(user);//sout
    }


    @Test
    public void testMapperfindUserByIdResultMap(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> user = mapper.findUserByIdResultMap(10);
        System.out.println(user);//sout
    }


    @Test
    public void testMapperfindUserListIfByVo(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user=new User();
        user.setUsername("张三");
        Order order=new Order();
        order.setNumber("1000012");
        QueryVo queryVo = new QueryVo();
        queryVo.setUser(user);
        queryVo.setOrder(order);
        List<User> user1 = mapper.findUserListIfByVo(queryVo);
        System.out.println(user1);//sout
    }


    @Test
    public void testMapperfindUserByIds(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(10);
        ids.add(31);
        QueryVo vo = new QueryVo();
        vo.setIds(ids);
        List<User> userByIds = mapper.findUserByIds(vo);
        System.out.println(userByIds);//sout
    }



    @Test
    public void testMapperfindUserByIdsList(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        ArrayList ids = new ArrayList();
//        ids.add(1);
//        ids.add(10);
//        ids.add(31);
        List<User> userByIdsList = mapper.findUserByIdsList(ids);
        System.out.println(userByIdsList);//sout
    }




    @Test
    public void findAllInfosByMap(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.findAllInfosByMap();
        for (User user: users){
            System.out.println(user);
            for (Order order : user.getOrderlist()) {
                System.out.println(order);
                for (OrderDetail orderdetail: order.getOrderDetails()) {
                    System.out.println(orderdetail+ "------"+orderdetail.getItem());
                }
            }
        }
    }

    @Test
    public void testOneCache(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        //一级缓存的意思是，同一个方法执行两边只有第一个方法会调用sql语句并且输出，但是第二个方法不调用直接输出

        //即两个方法的键相同
        //执行第一个方法后，若插入一条数据在这个表并提交，缓存就会被清除，然后第二个方法就会调用sql语句
        User user1 = mapper.findUserById(1);
        System.out.println(user1);
        //不会执行SQL，但是会从缓存中调用上一个相同方法中SQL的结果并输出
        User user2 = mapper.findUserById(1);
        System.out.println(user2);
    }

    @Test
    public void testOneCacheAndCommit(){
        UserMapper mapper = session.getMapper(UserMapper.class);
        //一级缓存的意思是，同一个方法执行两边只有第一个方法会调用sql语句并且输出，但是第二个方法不调用直接输出

        //即两个方法的键相同
        //执行第一个方法后，若插入一条数据在这个表并提交，缓存就会被清除，然后第二个方法就会调用sql语句
        User user1 = mapper.findUserById(1);
        System.out.println(user1);

        User user=new User();
        user.setUsername("李四");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("曹县");
        mapper.save(user);
        //—级缓存:session级别的，Mybatis默认的保存/删除/更新后，缓存被清除
        session.commit();

        User user2 = mapper.findUserById(1);
        System.out.println(user2);
    }





    @Test
    public void testTwoCache() throws IOException {

        //一级缓存的意思是，同一个方法执行两边只有第一个方法会调用sql语句并且输出，但是第二个方法不调用直接输出
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");

        //b)通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂。
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);

        // c)通过SqlSessionFactory创建SqlSession。
        SqlSession session1 = sessionFactory.openSession();
        SqlSession session2 = sessionFactory.openSession();
        SqlSession session3 = sessionFactory.openSession();

        UserMapper mapper1 = session1.getMapper(UserMapper.class);
        UserMapper mapper2 = session2.getMapper(UserMapper.class);
        UserMapper mapper3 = session3.getMapper(UserMapper.class);

        User user1 = mapper1.findUserById(1);
        System.out.println(user1);
        //写入二级缓存 //只有执行sqlSession.commit或sqlSession.close方法，一级缓存中的内容才会刷新到二级缓存中
        session1.close();

        User user=new User();
        user.setUsername("李四");
        user.setBirthday(new Date());
        user.setSex("男");
        user.setAddress("曹县");
        mapper3.save(user);
        //—级缓存:session级别的，Mybatis默认的保存/删除/更新后，缓存被清除
        session.commit();
        //查询的时候，先去二级缓存查，二级缓存没有，会查询一级缓存，一级缓存也没有，会查询数据库
        User user2 = mapper2.findUserById(1);
        //查询结果存入缓存时，先存入一级缓存，再存入二级缓存，只有执行sqlSession.commit或close方法，才能将一级缓存内容刷新到二级缓存中
        System.out.println(user2);

        session2.close();
        session3.close();
    }
}
